package com.msmall.community.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.community.ApplicationManager;
import com.msmall.community.roster.model.RosterItem;
import com.msmall.community.roster.service.RosterManager;

/**
 * 获得sender 对receiver的关系
 *
 */
public class RosterTag extends TagSupport{
	
	private static final long serialVersionUID = 5519842521452264132L;

	private final Log log = LogFactory.getLog(RosterTag.class);
	
	private Long sender;
	private Long receiver;
	
	public int doStartTag() throws JspException {
		return (SKIP_BODY);
	}

	public int doEndTag() throws JspException {
		RosterManager manager = (RosterManager) ApplicationManager.appContext.getBean("rosterManager");
		JspWriter writer = pageContext.getOut();
		String relation = "0";//默认没有关系
		try{
			RosterItem item = manager.getRosterItem(sender, receiver);
			if(item != null){
				relation = String.valueOf(item.getStandFor());
			}
		}catch(Exception e){
			log.error(e);
		}
		try {
			writer.print(relation);
		} catch (IOException e) {
			log.error(e);
		}
		return (SKIP_BODY);
	}
	
	public Long getSender() {
		return sender;
	}

	public void setSender(Long sender) {
		this.sender = sender;
	}

	public Long getReceiver() {
		return receiver;
	}

	public void setReceiver(Long receiver) {
		this.receiver = receiver;
	}
}
